package com.example.wheelpalma;

import java.util.Calendar;

import kankan.wheel.widget.OnWheelChangedListener;
import kankan.wheel.widget.WheelView;
import kankan.wheel.widget.adapters.NumericWheelAdapter;
import kankan.wheel.widget.adapters.ArrayWheelAdapter;
import kankan.wheel.widget.adapters.WheelViewAdapter;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
	Button btnHora, btnFecha;
	Calendar calendar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		btnHora = (Button) findViewById(R.id.button1);
		btnFecha = (Button) findViewById(R.id.button2);
		calendar = Calendar.getInstance();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	/**
	 * Adapter for numeric wheels. Highlights the current value.
	 */
	private class DateNumericAdapter extends NumericWheelAdapter {
		// Index of current item
		int currentItem;
		// Index of item to be highlighted
		int currentValue;

		/**
		 * Constructor
		 */
		public DateNumericAdapter(Context context, int minValue, int maxValue,
				int current) {
			super(context, minValue, maxValue);
			this.currentValue = current;
			setTextSize(20);
		}

		@Override
		protected void configureTextView(TextView view) {
			super.configureTextView(view);
			if (currentItem == currentValue) {
				view.setTextColor(0xFF0000F0);
			}
			view.setTypeface(Typeface.SANS_SERIF);
		}

		@Override
		public View getItem(int index, View cachedView, ViewGroup parent) {
			currentItem = index;
			return super.getItem(index, cachedView, parent);
		}
	}

	/**
	 * Adapter for string based wheel. Highlights the current value.
	 */
	private class DateArrayAdapter extends ArrayWheelAdapter<String> {
		// Index of current item
		int currentItem;
		// Index of item to be highlighted
		int currentValue;

		/**
		 * Constructor
		 */
		public DateArrayAdapter(Context context, String[] items, int current) {
			super(context, items);
			this.currentValue = current;
			setTextSize(20);
		}

		@Override
		protected void configureTextView(TextView view) {
			super.configureTextView(view);
			if (currentItem == currentValue) {
				view.setTextColor(0xFF0000F0);
			}
			view.setTypeface(Typeface.SANS_SERIF);
		}

		@Override
		public View getItem(int index, View cachedView, ViewGroup parent) {
			currentItem = index;
			return super.getItem(index, cachedView, parent);
		}
	}

	public void ClickHora(View v) {
		LayoutInflater inf = LayoutInflater.from(this);

		// layout of alert box contains
		View view = inf.inflate(R.layout.wheeltime, null);

		// hours wheel
		final WheelView hours = (WheelView) view.findViewById(R.id.hours);
		hours.setViewAdapter(new NumericWheelAdapter(this, 0, 23));
		hours.setCyclic(true);

		// minute wheel
		final WheelView mins = (WheelView) view.findViewById(R.id.minutes);
		mins.setViewAdapter(new NumericWheelAdapter(this, 0, 59));
		mins.setCyclic(true);

		new AlertDialog.Builder(this)
				.setMessage("Select Time")
				.setView(view)
				.setPositiveButton("Set",
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								// TODO Auto-generated method stub
								// write code of lines, whatever want to with
								// value of this timepicker
								int hora = hours.getCurrentItem();
								int minuto = mins.getCurrentItem();
								btnHora.setText(new StringBuilder()
										.append(hora).append(":")
										.append(minuto));
							}
						})

				.setNegativeButton("Cancel",
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								// TODO Auto-generated method stub
								return;
							}
						}).create().show();

		// set time
		hours.setCurrentItem(12);
		mins.setCurrentItem(45);
	}

	public void ClickFecha(View v) {
		LayoutInflater inf = LayoutInflater.from(this);

		// layout of alert box contains
		View view = inf.inflate(R.layout.wheeldate, null);

		final WheelView month = (WheelView) view.findViewById(R.id.month);
		final WheelView year = (WheelView) view.findViewById(R.id.year);
		final WheelView day = (WheelView) view.findViewById(R.id.day);

		OnWheelChangedListener listener = new OnWheelChangedListener() {
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				updateDays(year, month, day);
			}
		};

		// month
		int curMonth = calendar.get(Calendar.MONTH);
		String months[] = new String[] { "Enero", "Febrero", "Marzo", "Abril",
				"Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre",
				"Noviembre", "Deciembre" };
		month.setViewAdapter(new DateArrayAdapter(this, months, curMonth));
		// month.setCyclic(true);
		month.setCurrentItem(curMonth);
		month.addChangingListener(listener);

		// year
		final int curYear = calendar.get(Calendar.YEAR);
		WheelViewAdapter adapter;
		adapter = new DateNumericAdapter(this, curYear - 3, curYear + 10, 3);
		year.setViewAdapter(adapter);
		// year.setCyclic(true);
		year.setCurrentItem(3);
		year.addChangingListener(listener);

		// //day
		updateDays(year, month, day);
		// day.setCyclic(true);
		day.setCurrentItem(calendar.get(Calendar.DAY_OF_MONTH) - 1);

		new AlertDialog.Builder(this)
				.setMessage("Select Date")
				.setView(view)
				.setPositiveButton("Set",
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								// TODO Auto-generated method stub
								// write code of lines, whatever want to with
								// value of this timepicker
								int dia = day.getCurrentItem() + 1;
								int mes = month.getCurrentItem() + 1;
								int ano = (curYear - 3) + year.getCurrentItem();

								btnFecha.setText(new StringBuilder()
										.append(dia).append("/").append(mes)
										.append("/").append(ano).append(" "));
							}
						})

				.setNegativeButton("Cancel",
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								// TODO Auto-generated method stub
								return;
							}
						}).create().show();

	}

	/**
	 * Updates day wheel. Sets max days according to selected month and year
	 */
	void updateDays(WheelView year, WheelView month, WheelView day) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.YEAR,
				calendar.get(Calendar.YEAR) + year.getCurrentItem());
		calendar.set(Calendar.MONTH, month.getCurrentItem());

		int maxDays = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
		day.setViewAdapter(new DateNumericAdapter(this, 1, maxDays, calendar
				.get(Calendar.DAY_OF_MONTH) - 1));
		int curDay = Math.min(maxDays, day.getCurrentItem() + 1);
		day.setCurrentItem(curDay - 1, true);
	}

}
